<template>
  <div class="notice-create">
    <div class="page-header">
      <h2>{{ isEdit ? '编辑公告' : '发布公告' }}</h2>
      <div>
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSave">保存草稿</el-button>
        <el-button type="success" @click="handlePublish">发布</el-button>
      </div>
    </div>

    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item label="公告标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入公告标题" />
      </el-form-item>
      
      <el-form-item label="公告类型" prop="type">
        <el-input v-model="form.type" placeholder="请输入公告类型" />
      </el-form-item>
      
      <el-form-item label="是否置顶" prop="isTop">
        <el-switch v-model="form.isTop" />
      </el-form-item>
      
      <el-form-item label="是否紧急" prop="isUrgent">
        <el-switch v-model="form.isUrgent" />
      </el-form-item>
      
      <el-form-item label="公告内容" prop="content">
        <el-input
          v-model="form.content"
          type="textarea"
          :rows="10"
          placeholder="请输入公告内容"
        />
      </el-form-item>
      
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="form.priority" placeholder="请选择优先级">
          <el-option label="普通" :value="1" />
          <el-option label="重要" :value="2" />
          <el-option label="紧急" :value="3" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          v-model="form.publishTime"
          type="datetime"
          placeholder="请选择发布时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="form.expireTime"
          type="datetime"
          placeholder="请选择过期时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()

const isEdit = ref(false)
const formRef = ref()

const form = ref({
  title: '',
  type: '',
  isTop: false,
  isUrgent: false,
  content: '',
  priority: 1,
  publishTime: '',
  expireTime: ''
})

const rules = {
  title: [{ required: true, message: '请输入公告标题', trigger: 'blur' }],
  content: [{ required: true, message: '请输入公告内容', trigger: 'blur' }]
}

const handleCancel = () => {
  router.back()
}

const handleSave = async () => {
  try {
    await formRef.value.validate()
    // TODO: 调用保存草稿API
    ElMessage.success('保存成功')
    router.push('/notices/list')
  } catch (error) {
    ElMessage.error('保存失败')
  }
}

const handlePublish = async () => {
  try {
    await formRef.value.validate()
    // TODO: 调用发布API
    ElMessage.success('发布成功')
    router.push('/notices/list')
  } catch (error) {
    ElMessage.error('发布失败')
  }
}

onMounted(() => {
  const id = route.params.id
  if (id) {
    isEdit.value = true
    // TODO: 加载公告数据
  }
})
</script>

<style scoped>
.notice-create {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>